package list.ittest.Test.C_Service;

import list.ittest.Test.D_Dao.StudentDao;
import list.ittest.Test.E_Domain.Student;

import java.util.ArrayList;

public class  StudentService {
    StudentDao studentDao = new StudentDao();
    public void addStudent(Student stu) {
        studentDao.addStudent(stu);

    }

    public ArrayList<Student> findAllStudent() {
        ArrayList<Student> list = studentDao.findAllStudent();
        return list;
    }

    public boolean deleteStudent(String id) {
        ArrayList<Student>list = studentDao.findAllStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getId().equals(id)){
                studentDao.deleteStudent(i);
                return true;
            }
        }
        return false;
    }

    public boolean cheakId(String id) {
        ArrayList<Student>list = studentDao.findAllStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getId().equals(id)){
                return false;
            }
        }
        return true;
    }

    public void updateStudent(Student s) {
        ArrayList<Student>list = studentDao.findAllStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getId().equals(s.getId())){
                studentDao.updateStudent(i,s);
            }
        }
    }
}

